#include "StdAfx.h"

void PDFTemplates::makeT_PageSinglePhoto(PDFPage& page, const wstring& imgFile, HPDF_PAGESIZES size, bool optimize)
{
	page.Size = size;
	page.Margin = 0;
	
	HPDF_REAL width;
	HPDF_REAL height;
	FilesRefactorUtils::GetImageSize(&width, &height, imgFile);

	page.IsLandscape = width > height;
	page.OptimizeImages = optimize;
	HPDF_REAL w = page.IsLandscape? HPDF_PREDEFINED_PAGE_SIZES[size].y : HPDF_PREDEFINED_PAGE_SIZES[size].x;
	HPDF_REAL h = page.IsLandscape? HPDF_PREDEFINED_PAGE_SIZES[size].x : HPDF_PREDEFINED_PAGE_SIZES[size].y;

	page.ClearImages();
	page.AddImage(imgFile, false, 0, w, h, 0, 0);	
}
void PDFTemplates::T_PageSinglePhotoWallet(PDFPage& page, const wstring& imgFile, bool optimize)
{
	makeT_PageSinglePhoto(page, imgFile, PAGE_SIZE_WALLET, optimize);
}
void PDFTemplates::T_PageSinglePhoto3_5x5(PDFPage& page, const wstring& imgFile, bool optimize)
{
	makeT_PageSinglePhoto(page, imgFile, PAGE_SIZE_US3_5x5, optimize);
}
void PDFTemplates::T_PageSinglePhoto4x6(PDFPage& page, const wstring& imgFile, bool optimize)
{
	makeT_PageSinglePhoto(page, imgFile, PAGE_SIZE_US4x6, optimize);
}
void PDFTemplates::T_PageSinglePhoto5x7(PDFPage& page, const wstring& imgFile, bool optimize)
{
	makeT_PageSinglePhoto(page, imgFile, PAGE_SIZE_US5x7, optimize);
}
void PDFTemplates::T_PageSinglePhoto8x10(PDFPage& page, const wstring& imgFile, bool optimize)
{
	makeT_PageSinglePhoto(page, imgFile, PAGE_SIZE_US8x10, optimize);
}
void PDFTemplates::T_PageSinglePhoto9x12(PDFPage& page, const wstring& imgFile, bool optimize)
{
	makeT_PageSinglePhoto(page, imgFile, PAGE_SIZE_US9x12, optimize);
}

void PDFTemplates::T_320x464PageTwice8x10(PDFPage& page, bool picBorders, const wstring& imgFile1, const wstring& imgFile2, bool optimize)
{
	page.Size = PAGE_SIZE_320x464cm;
	page.Margin = 0;
	
	HPDF_REAL width;
	HPDF_REAL height;
	FilesRefactorUtils::GetImageSize(&width, &height, imgFile1);

	page.IsLandscape = width < height;
	page.OptimizeImages = optimize;
	HPDF_REAL w = page.IsLandscape? HPDF_PREDEFINED_PAGE_SIZES[PAGE_SIZE_US8x10].x : HPDF_PREDEFINED_PAGE_SIZES[PAGE_SIZE_US8x10].y;
	HPDF_REAL h = page.IsLandscape? HPDF_PREDEFINED_PAGE_SIZES[PAGE_SIZE_US8x10].y : HPDF_PREDEFINED_PAGE_SIZES[PAGE_SIZE_US8x10].x;
	HPDF_REAL x1 = page.IsLandscape?  40.0f : 93.6f;
	HPDF_REAL x2 = page.IsLandscape?  703.2744f : 93.6f;
	HPDF_REAL y1 = page.IsLandscape?  93.6f : 703.2744f;
	HPDF_REAL y2 = page.IsLandscape?  93.6f : 40.0f;

	page.ClearImages();
	page.AddImage(imgFile1, picBorders, 0, w, h, x1, y1);
	if (imgFile2 != L"")
	{
		page.AddImage(imgFile2, picBorders, 1, w, h, x2, y2);	
	}
}

void PDFTemplates::T_320x464PageQuad4x6(PDFPage& page, bool picBorders, bool isLandscape, const wstring& imgFile1, const wstring& imgFile2, 
		const wstring& imgFile3, const wstring& imgFile4, bool optimize)
{
	page.Size = PAGE_SIZE_320x464cm;
	page.Margin = 0;
	
	HPDF_REAL width;
	HPDF_REAL height;
	FilesRefactorUtils::GetImageSize(&width, &height, imgFile1);

	page.IsLandscape = isLandscape;
	page.OptimizeImages = optimize;
	HPDF_REAL w = HPDF_PREDEFINED_PAGE_SIZES[PAGE_SIZE_US4x6].y;
	HPDF_REAL h = HPDF_PREDEFINED_PAGE_SIZES[PAGE_SIZE_US4x6].x;
	HPDF_REAL x1 = page.IsLandscape?  115.2f : 237.6f;
	HPDF_REAL x2 = page.IsLandscape?  767.5704f : 237.6f;
	HPDF_REAL y1 = page.IsLandscape?  534.8f : 989.12f;
	HPDF_REAL y2 = page.IsLandscape?  534.8f : 670.88f;
	HPDF_REAL y3 = page.IsLandscape?  81.2f : 352.64f;
	HPDF_REAL y4 = page.IsLandscape?  81.2f : 33.8456f;

	page.ClearImages();
	page.AddImage(imgFile1, picBorders, 0, w, h, x1, y1);
	if (imgFile2 != L"")
	{
		page.AddImage(imgFile2, picBorders, 1, w, h, x2, y2);
	}
	if (imgFile3 != L"")
	{
		page.AddImage(imgFile3, picBorders, 2, w, h, x1, y3);
	}
	if (imgFile4 != L"")
	{
		page.AddImage(imgFile4, picBorders, 3, w, h, x2, y4);
	}
}

// retPDFFile - the returned PDFFile, rows - how many rows, cols - how many columns, isLandscape - portrait/landscape,
// pageSize - the size of the page from enum HPDF_PAGESIZES, picBorders - has borders?, listFiles - the paths to the jpg files,
// margins (optional... default 28) - width/height of margins around the page, gapPics (optional... default 56) - the gap between images
void PDFTemplates::T_DynamicTemplate(PDFFile& retPDFFile, int rows, int cols, bool isLandscape, HPDF_PAGESIZES pageSize, 
									 bool picBorders, const list<wstring>& listFiles, bool optimize,
									 HPDF_REAL margins, HPDF_REAL gapPics)
{
	list<wstring> listImgs(listFiles);

	HPDF_REAL pWidth = isLandscape? HPDF_PREDEFINED_PAGE_SIZES[pageSize].y : HPDF_PREDEFINED_PAGE_SIZES[pageSize].x;
	HPDF_REAL pHeight = isLandscape? HPDF_PREDEFINED_PAGE_SIZES[pageSize].x : HPDF_PREDEFINED_PAGE_SIZES[pageSize].y;

	HPDF_REAL iWidth = (pWidth/cols) - (margins + (gapPics / 2));
	HPDF_REAL iHeight = (pHeight/rows) - (margins + (gapPics / 2));

	HPDF_REAL x = margins;
	HPDF_REAL y = pHeight - (iHeight + margins);
	HPDF_REAL xOffset = iWidth + gapPics;
	HPDF_REAL yOffset = gapPics + iHeight;
	int imgsOnPage = rows * cols;
	size_t totalPages = listImgs.size() / imgsOnPage;
	totalPages += (listImgs.size() % imgsOnPage != 0)?1:0;
	for(size_t j=0; j<totalPages; j++)
	{
		PDFPage pg;
		pg.IsLandscape = isLandscape;
		pg.Margin = 0;
		pg.Size = pageSize;
		pg.OptimizeImages = optimize;

		int idx = 0;
		for(int i=0; (i<imgsOnPage) && (listImgs.size()>0); i++)
		{
			IMAGE_DEFINITIONS id;
			id.DrawBorders = picBorders;
			id.FilePath = listImgs.front();
			id.ImageIdx = idx++;

			// calculting image size
			FilesRefactorUtils::GetImageSizeInRect(&(id.Width), &(id.Height), iWidth, iHeight, listImgs.front());

			// calculating image location
			HPDF_REAL xR = (iWidth-id.Width) / 2.0f;
			HPDF_REAL yR = (iHeight-id.Height) / 2.0f;
			id.LocX = x+xR;
			id.LocY = y+yR;

			x += xOffset;
			if (x>=pWidth)
			{
				x = margins;
				y -= yOffset;
			}

			pg.AddImage(id);
			listImgs.pop_front();
		}
		retPDFFile.AddPage(pg);

		x = margins;
		y = pHeight - (iHeight + margins);
	}

}